package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.annotation.RequestLimit;
import cn.zjutleo.village_server.dto.SendCodeDto;
import cn.zjutleo.village_server.exception.apiException.smsException.MessageSendException;
import cn.zjutleo.village_server.service.SmsService;
import cn.zjutleo.village_server.vo.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author : Qin Zhenghan
 * @date : Created in 2021/8/23
 * @description: 腾讯云SMS短信控制类
 */
@Api(tags = "腾讯云SMS短信控制类")
@RestController
@RequestMapping("/sms")
public class SmsController {
    @Resource
    private SmsService smsService;

    /**
     * 发送短信验证码
     *
     * @param sendCodeDto 发送短信验证码DTO
     * @return 异常信息
     * @throws MessageSendException 短信发送异常
     */
    @ApiOperation("发送手机验证码")
    @RequestLimit(amount = 5, time = 300)
    @PostMapping("/send")
    public ResponseBean<Object> sendCode(@RequestBody SendCodeDto sendCodeDto) throws MessageSendException {
        boolean isSent = smsService.sendMessage(sendCodeDto.getPhone(), sendCodeDto.getType());
        if (isSent) {
            return new ResponseBean<>(200, "验证码发送成功", null);
        } else {
            throw new MessageSendException();
        }
    }
}
